package com.app.controller;

import com.app.aop.has.HasRedissonLock;
import com.app.aop.has.HasRole;
import com.app.domain.bo.NoticeBo;
import com.app.domain.page.Page;
import com.app.domain.vo.NoticeVo;
import com.app.framework.common.R;
import com.app.service.NoticeService;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 公告控制器
 */
@RestController
@RequestMapping("/api/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    /**
     * 公告列表
     *
     * @param bo 入参
     */
    @PostMapping("/list")
    public R<Page<NoticeVo>> lists(@RequestBody NoticeBo bo) {
        return R.ok(noticeService.lists(bo));
    }

    /**
     * 添加公告
     *
     * @param bo 入参
     */
    @HasRole()
    @HasRedissonLock("save_notice")
    @PostMapping("/save")
    public R<?> saveOne(@RequestBody NoticeBo bo) {
        noticeService.saveOne(bo);
        return R.ok(null, "添加成功");
    }

    /**
     * 修改公告
     *
     * @param bo 入参
     */
    @HasRole()
    @HasRedissonLock("update_notice")
    @PostMapping("/update")
    public R<?> updateOne(@RequestBody NoticeBo bo) {
        Assert.notNull(bo.getId(), "公告ID不能为空");
        noticeService.updateOne(bo);
        return R.ok(null, "修改成功");
    }

    /**
     * 删除公告
     *
     * @param noticeId 公告ID
     */
    @HasRole()
    @HasRedissonLock("delete_notice")
    @PostMapping("/delete/{noticeId}")
    public R<?> deleteOne(@PathVariable("noticeId") Long noticeId) {
        noticeService.deleteOne(noticeId);
        return R.ok(null, "删除成功");
    }

}